package features.advance.leetcode.tree.easy;

import features.advance.leetcode.tree.model.TreeNode;
import features.advance.leetcode.util.TreeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LIN
 * @date 2022-07-30 17:21
 *
 *  257. 二叉树的所有路径
 *
 *  难度：简单
 *
 * 给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。
 *
 * 叶子节点 是指没有子节点的节点。
 *
 *
 * 示例 1：
 *
 *
 * 输入：root = [1,2,3,null,5]
 * 输出：["1->2->5","1->3"]
 * 示例 2：
 *
 * 输入：root = [1]
 * 输出：["1"]
 *
 *
 * 提示：
 *
 * 树中节点的数目在范围 [1, 100] 内
 * -100 <= Node.val <= 100
 *
 *
 */
public class Solution257 {

    public static void main(String[] args) {
        Solution solution = new Solution() {
        };

        TreeNode<Integer> root = TreeUtil.stringToTreeNode("[1,2,3,null,5]");
        List<String> list = solution.binaryTreePaths(root);
        System.out.println(list);

    }

    static class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> list = new ArrayList();
            String path = "";
            recursion(root,path,list);
            System.out.print(path);
            return list;
        }


        public void recursion(TreeNode root, String path,List<String> list){
            if(root == null){
//                 System.out.print(path);
                return;
            }
            if(!"".equals(path)){
                path+="->"+root.val;
            }else{
                path+=root.val;
            }
            if(isLeaf(root)){
                list.add(path);
            }
            recursion(root.left,path,list);
            recursion(root.right,path,list);
        }

        private boolean isLeaf(TreeNode root) {
            return root == null || root.left == null && root.right == null;
        }
    }
}
